package com.ruoyi.web.controller.modules;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.domain.stock.Storage;
import com.ruoyi.web.pojo.dto.stock.StorageExcel;
import com.ruoyi.web.pojo.dto.stock.StorageRep;
import com.ruoyi.web.pojo.request.stock.StorageAddReq;
import com.ruoyi.web.service.stock.IStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 入库明细Controller
 *
 * @author zq
 * &#064;date  2024-01-06
 */
@Api(value = "入库明细",tags = "入库明细")
@RestController
@RequestMapping("/storage")
public class StorageController extends BaseController{

    private final IStorageService storageService;
    private final ISysUserService sysUserService;

    public StorageController(IStorageService storageService, ISysUserService sysUserService) {
        this.storageService = storageService;
        this.sysUserService = sysUserService;
    }

    /**
     * 查询入库明细列表
     */
    @ApiOperation("查询入库明细列表")
    @GetMapping("/list")
    public TableDataInfo list(Storage storage)
    {
        startPage();
        List<StorageRep> list = storageService.selectStorageList(storage);
        if (CollectionUtils.isEmpty(list)){
            return  getDataTable(new ArrayList<>());
        }
        //list.forEach(s->s.setUserId(SecurityUtils.getLoginUser().getUser().getPhonenumber()));
        return getDataTable(list);
    }

    /**
     * 导出入库明细列表
     */
    @ApiOperation("导出入库明细列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody Storage storage) throws Exception {
        List<StorageRep> list = storageService.selectStorageList(storage);
        if (CollectionUtils.isEmpty(list)){
            return;
        }

        List<StorageExcel> collect = list.stream().map(StorageExcel::new).collect(Collectors.toList());
        ExcelUtil<StorageExcel> util = new ExcelUtil<>(StorageExcel.class);
//        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("入库明细数据"+ DateUtils.datePath() +".xlsx", StandardCharsets.UTF_8.name()));
        util.exportExcel(response, collect, "入库明细数据");
    }

    /**
     * 获取入库明细详细信息
     */
    @ApiOperation("获取入库明细详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Storage storage = storageService.selectStorageById(id);
        if (Objects.isNull(storage)){
            return success();
        }
        StorageRep rep = new StorageRep(storage);
        if (Objects.nonNull(storage.getUserId())){
            SysUser sysUser = sysUserService.selectUserById(storage.getUserId());
            if (Objects.nonNull(sysUser)){
                rep.setUserId(sysUser.getUserName());
                rep.setCreateBy(sysUser.getNickName());
            }
        }
        return success(rep);
    }

    /**
     * 新增入库明细
     */
    @ApiOperation("新增入库明细")
    @PostMapping("/insert")
    @RateLimiter(time = 1,count = 1)
    public AjaxResult add(@RequestBody StorageAddReq req)
    {

        return toAjax(storageService.insertStorage(Storage.toStorage(req)));
    }

}

